#include "sock.hpp"
void *Read(void *arg)
{
    pthread_detach(pthread_self());
    Data *ptr = (Data *)arg;
    char buffer[_BUFFER_NUN];
    sockaddr_in client;
    socklen_t len = sizeof(client);
    while (true)
    {
        memset(buffer, 0, _BUFFER_NUN);
        recvfrom(ptr->_sock, buffer, _BUFFER_NUN - 1, 0, (sockaddr *)(&client), &len);
        std::cout << buffer << std::endl;
    }
    return nullptr;
}
int main(int args, char *argv[])
{
    if (args < 2)
        return -1;
    Sock s;
    Data sock = s.Creat(SOCK_DGRAM);
    sockaddr_in serve;
    serve.sin_addr.s_addr = inet_addr(argv[1]);
    serve.sin_family = AF_INET;
    serve.sin_port = htons(atoi(argv[2]));
    char buffer[_BUFFER_NUN];
    pthread_t tid;
    pthread_create(&tid, nullptr, Read, &sock);
    while (true)
    {
        std::cout << "输入" << std::endl;
        std::cin >> buffer;
        sendto(sock._sock, buffer, _BUFFER_NUN, 0, (sockaddr *)(&serve), sizeof(serve));
    }
    return 0;
}